package com.example.myone.Fragment;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import com.example.myone.Activity.LoginActivity;
import com.example.myone.Activity.RegisterActivity;
import com.example.myone.Pager.MyPager;
import com.example.myone.R;
import com.example.myone.Util.LoginInfoUtil;

@RequiresApi(api = Build.VERSION_CODES.O)
public class LoginFragment extends Fragment {
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (LoginInfoUtil.isLogin(getContext())) {
            MyPager.replaceFragment(new MyFragment());
        }
        View view = inflater.inflate(R.layout.my_pager_login_fragment, container, false);
        View registerButton = view.findViewById(R.id.to_register_button);
        View loginButton = view.findViewById(R.id.login_button);
        CheckBox checkBox = view.findViewById(R.id.checkbox);
        registerButton.setOnClickListener(v -> {
            if (checkBox.isChecked()) {
                Intent intent = new Intent(v.getContext(), RegisterActivity.class);
                v.getContext().startActivity(intent);
            } else {
                Toast.makeText(getContext(), "登录/注册前请先阅读并同意相关协议", Toast.LENGTH_SHORT).show();
            }
        });
        loginButton.setOnClickListener(v -> {
            if (checkBox.isChecked()) {
                Intent intent = new Intent(v.getContext(), LoginActivity.class);
                v.getContext().startActivity(intent);
            } else {
                Toast.makeText(getContext(), "登录/注册前请先阅读并同意相关协议", Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }


}
